package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:下月拜访计划模块计划内容解析类
 * @author wuz  @date 2011-5-25 08:19:42
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class VisitPlanBeanParser {
	public static List<VisitPlanBean> parseXML(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		VisitPlanBean bean = null;
		MyWayBean myWayBean = null;
		VisitPlanWorkTypeBean visitPlanWorkTypeBean = null;
		List<VisitPlanBean> list = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<VisitPlanBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						bean = new VisitPlanBean();
						myWayBean = new MyWayBean();
						visitPlanWorkTypeBean = new VisitPlanWorkTypeBean();
					} else if (null != list && null != bean) {
						if ("VSDATE".equals(parser.getName())) {
							bean.setVsdate(Integer.parseInt(parser.nextText()));
						} else if ("WK".equals(parser.getName())) {
							bean.setWk(parser.nextText());
						} else if ("WAYID".equals(parser.getName())) {
							myWayBean.setWayid(parser.nextText());
						} else if ("WAYNAME".equals(parser.getName())) {
							myWayBean.setWayname(parser.nextText());
						} else if ("WORKTYPE".equals(parser.getName())) {
							visitPlanWorkTypeBean
									.setWorkTypeId(Integer.parseInt(parser.nextText()));
						} else if ("WORKTYPENAME".equals(parser.getName())) {
							visitPlanWorkTypeBean.setWorkTypeName(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						if (null != list && null != bean) {
							bean.setMyWayBean(myWayBean);
							bean.setVisitPlanWorkTypeBean(visitPlanWorkTypeBean);
							list.add(bean);
							bean = null;
							myWayBean = null;
							visitPlanWorkTypeBean = null;
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}

	public static String createXMLPlandetail(List<VisitPlanBean> list, String monthid, Writer writer)
			throws Exception {
		XmlSerializer xmlSerializer = Xml.newSerializer();

		xmlSerializer.setOutput(writer);
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, list.size() + "");
		String year = monthid.substring(0, 4);
		String month = monthid.substring(4, 6);

		for (int i = 0; i < list.size(); i++) {
			VisitPlanBean bean = list.get(i);
			xmlSerializer.startTag("", XMLStatic.RECORDS);
			xmlSerializer.startTag("", XMLStatic.VSDATE);
			if (bean.getVsdate() < 10) {
				xmlSerializer.text(year + "-" + month + "-" + "0" + bean.getVsdate());
			} else {
				xmlSerializer.text(year + "-" + month + "-" + bean.getVsdate());
			}
			xmlSerializer.endTag("", XMLStatic.VSDATE);

			xmlSerializer.startTag("", XMLStatic.WAYID);
			if (null != bean.getMyWayBean().getWayid()) {
				xmlSerializer.text(bean.getMyWayBean().getWayid());
			} else {
				xmlSerializer.text("");
			}
			xmlSerializer.endTag("", XMLStatic.WAYID);

			xmlSerializer.startTag("", XMLStatic.WORKTYPE);
			if (null != bean.getVisitPlanWorkTypeBean().getWorkTypeName()) {
				xmlSerializer.text("" + bean.getVisitPlanWorkTypeBean().getWorkTypeId());
			} else {
				xmlSerializer.text("");
			}
			xmlSerializer.endTag("", XMLStatic.WORKTYPE);

			xmlSerializer.endTag("", XMLStatic.RECORDS);
		}

		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace(
				"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
}
